package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.model.SkuInfo;
import com.atguigu.gmall.product.service.SkuManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author: atguigu
 * @create: 2022-12-26 11:52
 */
@RestController
@RequestMapping("/admin/product")
public class SkuManageController {

    @Autowired
    private SkuManageService skuManageService;


    /**
     * 保存SKU信息
     *
     * @param skuInfo SKU相关信息
     * @return
     */
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo) {
        skuManageService.saveSkuInfo(skuInfo);
        return Result.ok();
    }


    /**
     * 查询商品SKU列表
     *
     * @param page    页面
     * @param limit   页大小
     * @param skuInfo 查询条件
     * @return
     */
    @GetMapping("/list/{page}/{limit}")
    public Result getSkuInfoPage(@PathVariable Long page,
                                 @PathVariable Long limit,
                                 SkuInfo skuInfo) {
        //  构建一个Page对象
        Page<SkuInfo> skuInfoPage = new Page<>(page, limit);
        //  调用服务层方法！
        IPage iPage = skuManageService.getSkuInfoPage(skuInfoPage, skuInfo);
        return Result.ok(iPage);
    }


    /**
     * 将指定SKU进行上架
     * @param skuId
     * @return
     */
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable("skuId") Long skuId){
        skuManageService.onSale(skuId);
        return Result.ok();
    }


    /**
     * 将指定SKU进行下架
     * @param skuId
     * @return
     */
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable("skuId") Long skuId){
        skuManageService.cancelSale(skuId);
        return Result.ok();
    }


}
